﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GL.Tools;
using System.Web.SessionState;

namespace WAD.WEB.AppCode.Sms
{
    public class SMSHelper
    {
        /// <summary>
        /// 验证码发送的类型-注册
        /// </summary>
        private const int TYPE_REGIST = 1;

        /// <summary>
        /// 验证码发送的类型-登录
        /// </summary>
        private const int TYPE_LOGIN = 2;

        /// <summary>
        /// 验证码发送的类型-修改手机号
        /// </summary>
        private const int TYPE_MODIFY = 4;

        /// <summary>
        /// 生成随机code
        /// </summary>
        /// <returns></returns>
        private static String MakeCode()
        {
            return StringHelper.RandomNumber(4);
        }

        /// <summary>
        /// 生成并发送验证码
        /// </summary>
        /// <param name="phone">接收验证码的手机号</param>
        /// <param name="type">验证码发送类型</param>
        /// <returns></returns>
        private static bool SendCode(String phone, int type)
        {
            HttpSessionState session = HttpContext.Current.Session;

            //得到已经保存到session的验证码信息
            SMSCodeBean bean = (SMSCodeBean)session[WJXConfig.Session.SMS_CODE];
            if (bean != null && (DateTime.Now - bean.Time).TotalMilliseconds < WJXConfig.Cfg.SMS_CODE_INTERVAL)
            {
                //验证码发送过于频繁
                return false;
            }

            String code = MakeCode();
            bool isSuccess = false;

            switch (type)
            {
                case TYPE_REGIST:
                    isSuccess = AliSMS.SendRegistCode(phone, code);
                    break;
                case TYPE_LOGIN:
                    isSuccess = AliSMS.SendLoginCode(phone, code);
                    break;
                case TYPE_MODIFY:
                    isSuccess = AliSMS.SendModifyCode(phone, code);
                    break;
                default:
                    break;
            }

            if (isSuccess)
            {

                //封装验证码信息并保存到session
                SMSCodeBean newCode = new SMSCodeBean { Code = code, Phone = phone, Time = DateTime.Now };
                session.Add(WJXConfig.Session.SMS_CODE, newCode);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 验证码验证
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public static bool ValidateCode(String phone, String code)
        {
            HttpSessionState session = HttpContext.Current.Session;

            //得到已经保存到session的验证码信息
            SMSCodeBean bean = (SMSCodeBean)session[WJXConfig.Session.SMS_CODE];
            if (bean == null || !bean.Code.Equals(code))
            {
                //验证码不正确或已过期
                return false;
            }
            else
            {
                //验证成功清除该session
                session.Remove(WJXConfig.Session.SMS_CODE);
                return true;
            }
        }

        /// <summary>
        /// 注册,这里没有验证手机号,请在调用此方法之前验证手机号是否合法
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool Regist(String phone)
        {
            return SendCode(phone, TYPE_REGIST);
        }

        /// <summary>
        /// 登录,这里没有验证手机号,请在调用此方法之前验证手机号是否合法
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool Login(String phone)
        {
            return SendCode(phone, TYPE_LOGIN);
        }

        /// <summary>
        /// 修改手机号,这里没有验证手机号,请在调用此方法之前验证手机号是否合法
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool Modify(String phone)
        {
            return SendCode(phone, TYPE_MODIFY);
        }
    }
}